<#include "/@inc/funcMacros.ftl">
<#include "/@inc/copyright.txt">
<#assign className = TriaGram + Prefix + "CatalogMain">
<#assign intfName = TriaGram + "I"+ Prefix + LateType>
<#assign IDLName = TriaGram + "IA" + Prefix + LateType>
<#assign baseName = TriaGram + Prefix>
<@pp.renameOutputFile name=className+".cpp">
</@pp.renameOutputFile>

//----System framework
#include "CATUnicodeString.h"

//----ObjectModelerBase
#include "CATSessionServices.h"

//----ObjectSpecsModeler
#include "CATCatalogFactoryServices.h"
#include "CATICatalog.h"
#include "CATISpecObject.h"
#include "CATISpecAttribute.h"

//----Utility function 
static HRESULT CreateStartups( CATICatalog * pICatalog );

//-----------------------------------------------------------------------------
// main() 
//-----------------------------------------------------------------------------
int main( int argc, char** argv )
{
    //----Create Session
	char         * pchSessionName = "${className}";
	CATSession   * pSession       = NULL;
	HRESULT hr = Create_Session(pchSessionName, pSession);

	if (FAILED(hr))
	{
		return 1;
	}

	//"Step 1: Session Creation OK");
    //----

	//----Catalog creation
	CATUnicodeString   ustrFileName = "{baseName}";
	CATICatalog      * pICatalog    = NULL;
    hr = ::CreateCatalog(&ustrFileName, &pICatalog);
    
	if (FAILED(hr) || (NULL == pICatalog))
	{
		// ("Catalog Creation has FAILED");
		return 1;
	}
	
	//"Step 2: Catalog Creation OK");
	//----Set Client Id
	CATUnicodeString ustrClientID = "{baseName}";
	hr = pICatalog->SetClientId(&ustrClientID);
	if (FAILED(hr))
	{
		pICatalog->Release();
		pICatalog = NULL;
		// ("SetClientId has FAILED");
		return 1;
	}

	//"Step 3: Set Client Id OK");
	//----StartUps creation 
    hr = CreateStartups( pICatalog );
	if (FAILED(hr))
	{
		pICatalog->Release();
		pICatalog = NULL;
		// ("CreateStartups has FAILED");
		return 1;
	}

    //"Step 4: StartUps Creation OK");
	//----Save Catalog
	hr = ::SaveCatalog (&pICatalog, &ustrFileName);
	if ( NULL != pICatalog)
	{
		pICatalog->Release();
		pICatalog = NULL;
	}

	if (FAILED(hr))
	{
		// ("Catalog save has FAILED");
		return 1;
	}
    
	//"Step 5: Document saved OK");	
	//----Delete Session
	hr = ::Delete_Session(pchSessionName);
	if (FAILED(hr))
	{
		// ("Session destruction has FAILED");
		return 1;
	}

	//"Step 6: Session destruction OK");
	return 0;
}


//-----------------------------------------------------------------------------
// CreateStartups() 
//-----------------------------------------------------------------------------
static HRESULT CreateStartups( CATICatalog *  pICatalog )
{
	//"{ Start of: CreateStartups()");

	if ( NULL == pICatalog ) {
		// ("Invalid input argument");
		return E_FAIL;
	}

    //----Create the Startup for "NEOPixRootElement" component
	CATISpecObject   * pISpecObject = NULL;
	CATUnicodeString   ustrName  = "${SuperType}";
	CATUnicodeString   ustrType  = "${LateType}";
	HRESULT hr = pICatalog->CreateSUInCatalog((CATBaseUnknown **) &pISpecObject, 
		                                      &ustrName, &ustrType,
											  "CATISpecObject");
	
	if (FAILED(hr))
	{
		// ("Creation PixRootElement startup has FAILED");
		return E_FAIL;
	}

	CATISpecAttribute * pISpecAttribute = NULL ;	
	  <#list Attributes as attrib>
	   <@ADD_ATTRIBUTE "pISpecObject" attrib Prefix />
	  </#list>

	return S_OK;
}
